package org.example.view;


import org.example.util.PropertiesUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author TonySong
 * @date 2025/8/4 0004
 * @time 10:35
 */
public class Login extends JFrame {
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JTextField txtUsername;
    private JPasswordField passwordField;
    private JButton btnLogin;
    private JButton btnCancel;

    {
        this.lblUsername=new JLabel("请收入账号");
        this.lblPassword=new JLabel("请输入密码");
        this.txtUsername=new JTextField(20);
        this.passwordField=new JPasswordField(20);
        this.btnLogin=new JButton("登录");
        this.btnCancel=new JButton("取消");
        this.setLayout(new FlowLayout());
        this.add(this.lblUsername);
        this.add(this.txtUsername);

        this.add(this.lblPassword);
        this.add(passwordField);

        this.add(this.btnLogin);
        this.add(this.btnCancel);
        this.setTitle("用户登录");
        this.setSize(300,150);
        Toolkit toolkit =Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        int x= (int) (width -this.getWidth())/2;
        int y= (int) (height -this.getHeight())/2;
        this.setLocation(x,y);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.btnCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //JOptionPane.showMessageDialog(null,"要退出吗？");
                int dialog = JOptionPane.showConfirmDialog(null, "确认退出吗？",
                        "提示", JOptionPane.OK_CANCEL_OPTION);
                if(JOptionPane.OK_OPTION == dialog){
                     JOptionPane.showMessageDialog(null,"谢谢，欢迎下次使用！");
                     System.exit(0);
                }
            }
        });

        this.btnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //进行为空的判断
                String username = txtUsername.getText().trim();
                char[] password1 = passwordField.getPassword();
                if( username.length()==0 || password1.length==0){
                    JOptionPane.showMessageDialog(null,"请收入账号或密码");
                }else {
                    //获取账号和密码
                    String password = new String(password1);
                    File file = new File("src/main/resources/admin.properties");
                    //properties内容进行校验
                    if (PropertiesUtil.checkUser(file, username, password)) {
                        JOptionPane.showMessageDialog(null, "登录成功！");
                        //跳转到主界面
                       MovieFrame.MOVIE_FRAME.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "对不起，账号或密码错误，登录失败！");
                    }
                }

            }
        });
    }

    public static void main(String[] args) {
        new Login().setVisible(true);
    }
}
